import { exists } from "https://deno.land/std@0.200.0/fs/mod.ts"; import { build, stop } from 'https://deno.land/x/esbuild@v0.19.2/mod.js'; import type { BuildOptions, BuildResult } from 'https://deno.land/x/esbuild@v0.19.2/mod.js'; // import { cache } from 'https://raw.githubusercontent.com/takker99/esbuild-plugin-cache/master/deno/mod.ts'; import { cache } from './mod.ts'; // 特定のpropertyを削るやつ type Omit = Pick>; export async function run(filename: string, imports: {[key: string]: string}, {external, ...rest }: Omit) { let useTempFile = false; let result: BuildResult | undefined = undefined; try { if (/^https?:\/\//.test(filename)) { const tempname = `index-${Math.random()}.ts`; await Deno.writeTextFile(tempname, `import '${filename}';export * from '${filename}';`); filename = tempname; useTempFile = true; } const options: BuildOptions = { entryPoints: [filename], platform: 'neutral', plugins: [cache({directory: './cache', importmap: {imports}})], external: Array.isArray(external) ? external : (external ? [external] : []), ...rest }; result = await build(options); stop(); } catch(e) { throw e; }finally { //後始末 if (useTempFile) await Deno.remove(filename); if (await exists('./cache')) await Deno.remove('./cache', { recursive: true }); } return result; }